package com.yihuier.yihuier.Mapper.UserManager;

import com.yihuier.yihuier.Entity.UserManager.Index;
import com.yihuier.yihuier.Entity.UserManager.User;
import org.apache.ibatis.annotations.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component(value="Mapper")
@Mapper
public interface UserMapper {
    /**
     * 加载最新的用户
     * @param offset 偏移量
     * @param rows 结果集中多少个用户
     * @return 用户的结果集
     */
    @Results({
            @Result(property = "user_id" , column = "user_id"),
            @Result(property = "user_account" , column = "user_account"),
            @Result(property = "user_name" , column = "user_name"),
            @Result(property = "user_password" , column = "user_password"),
            @Result(property = "user_face_identify_matrix" , column = "user_face_identify_matrix"),
            @Result(property = "user_phone" , column = "user_phone"),
            @Result(property = "user_email" , column = "user_email"),
            @Result(property = "user_wechat" , column = "user_wechat"),
            @Result(property = "user_privileges" , column = "user_privileges"),
            @Result(property = "user_headpic_dir" , column = "user_headpic_dir"),
            @Result(property = "user_status" , column = "user_status"),
            @Result(property = "answer_1_no" , column = "user_security_question1_no"),
            @Result(property = "answer_1" , column = "user_security_question1_answer"),
            @Result(property = "answer_2_no" , column = "user_security_question2_no"),
            @Result(property = "answer_2" , column = "user_security_question2_answer"),
            @Result(property = "answer_3_no" , column = "user_security_question3_no"),
            @Result(property = "answer_3" , column = "user_security_question3_answer"),
    })
    @Select("SELECT user_id,user_account,user_name,user_password,user_face_identify_matrix,user_phone,user_email,user_wechat,user_privileges,user_headpic_dir,user_status,user_security_question1_no,user_security_question1_answer,user_security_question2_no,user_security_question2_answer,user_security_question3_no,user_security_question3_answer FROM `bz_user` ORDER BY user_id DESC LIMIT #{offset},#{rows};")
    List<User> selectLatestUsers(@Param("offset") int offset,@Param("rows") int rows);

    /**
     * 通过用户账号找到用户
     * @param user_account 用户账号
     * @return 用户ID号
     */
    @Select("SELECT user_id FROM `bz_user` WHERE user_account = #{user_account};")
    String selectUserByUserAccount(@Param("user_account") String user_account);

    /**
     * 通过用户手机号找到用户
     * @param user_phone 用户手机号
     * @return 用户ID号
     */
    @Select("SELECT user_id FROM `bz_user` WHERE user_phone = #{user_phone};")
    String selectUserByUserPhone(@Param("user_phone") String user_phone);


    /**
     * 通过用户邮箱找到用户
     * @param user_email 用户邮箱
     * @return 用户ID号
     */
    @Select("SELECT  user_id FROM `bz_user` WHERE user_email = #{user_email};")
    String selectUserByUserEmail(@Param("user_email") String user_email);

    /**
     * 通过用户微信号找到用户
     * @param user_wechat 用户微信号
     * @return 用户ID号
     */
    @Select("SELECT  user_id FROM `bz_user` WHERE user_wechat = #{user_wechat};")
    String selectUserByUserWechat(@Param("user_wechat") String user_wechat);

    /**
     * 通过用户id找到用户头像url
     * @param user_id 用户编号
     * @return 用户头像url号
     */
    @Results({
            @Result(property = "user_id" , column = "user_id"),
            @Result(property = "user_name" , column = "user_name"),
            @Result(property = "user_headpic_dir" , column = "user_headpic_dir"),
            @Result(property = "user_email" , column = "user_email"),
    })
    @Select("SELECT  user_id,user_headpic_dir,user_name,user_email FROM `bz_user` WHERE user_id = #{user_id} AND user_status = 1;")
    User selectUserinfomationByUserid(@Param("user_id") int user_id);

    @Results({
            @Result(property = "user_id" , column = "user_id"),
            @Result(property = "user_name" , column = "user_name"),
            @Result(property = "user_headpic_dir" , column = "user_headpic_dir"),
            @Result(property = "user_email" , column = "user_email"),
            @Result(property = "user_status" , column = "user_status"),
    })
    @Select("SELECT  user_id,user_headpic_dir,user_name,user_email,user_status FROM `bz_user` WHERE user_id = #{user_id};")
    User selectUserinfomationByUseridForManager(@Param("user_id") int user_id);

    /**
     * 用户登录成功则加载用户信息到内存
     * @param user_id 用户ID
     * @param user_password 用户密码
     * @return 用户内存模型
     */
    @Results({
            @Result(property = "user_id" , column = "user_id"),
            @Result(property = "user_account" , column = "user_account"),
            @Result(property = "user_name" , column = "user_name"),
            @Result(property = "user_password" , column = "user_password"),
            @Result(property = "user_face_identify_matrix" , column = "user_face_identify_matrix"),
            @Result(property = "user_phone" , column = "user_phone"),
            @Result(property = "user_email" , column = "user_email"),
            @Result(property = "user_wechat" , column = "user_wechat"),
            @Result(property = "user_privileges" , column = "user_privileges"),
            @Result(property = "user_headpic_dir" , column = "user_headpic_dir"),
            @Result(property = "user_status" , column = "user_status"),
    })
    @Select("SELECT user_id,user_account,user_name,user_password,user_face_identify_matrix,user_phone,user_email,user_wechat,user_privileges,user_headpic_dir,user_status FROM `bz_user` WHERE user_id = #{user_id} AND user_password = #{user_password} AND (user_status = 1 OR user_status = 4);")
    User loadUserByUserAccount(@Param("user_id") String user_id,@Param("user_password") String user_password);

    /**
     * 用户登录成功则加载用户信息到内存
     * @param user_wechat 用户微信openid
     * @return 用户内存模型
     */
    @Results({
            @Result(property = "user_id" , column = "user_id"),
            @Result(property = "user_account" , column = "user_account"),
            @Result(property = "user_name" , column = "user_name"),
            @Result(property = "user_password" , column = "user_password"),
            @Result(property = "user_face_identify_matrix" , column = "user_face_identify_matrix"),
            @Result(property = "user_phone" , column = "user_phone"),
            @Result(property = "user_email" , column = "user_email"),
            @Result(property = "user_wechat" , column = "user_wechat"),
            @Result(property = "user_privileges" , column = "user_privileges"),
            @Result(property = "user_headpic_dir" , column = "user_headpic_dir"),
            @Result(property = "user_status" , column = "user_status"),
    })
    @Select("SELECT user_id,user_account,user_name,user_password,user_face_identify_matrix,user_phone,user_email,user_wechat,user_privileges,user_headpic_dir,user_status FROM `bz_user` WHERE user_wechat = #{user_wechat} AND user_status = 1;")
    User loadUserByUserWechat(@Param("user_wechat") String user_wechat);

    @Update("UPDATE bz_user SET user_wechat = #{user_wechat} WHERE user_id = #{user_id} AND user_password = #{user_password} AND user_status = 1; ")
    int bingdingOpenidToUser(@Param("user_wechat") String user_wechat,@Param("user_id") String user_id,@Param("user_password") String user_password);

    @Update("UPDATE bz_user SET user_password = #{user_newpassword} WHERE user_id = #{user_id} AND user_password = #{user_oldpassword} AND user_status = 1; ")
    int updateUserPassword(@Param("user_id") int user_id,@Param("user_oldpassword") String user_oldpassword,@Param("user_newpassword") String user_newpassword);

    @Update("UPDATE bz_user SET user_password = #{user_password} WHERE user_id = #{user_id} AND user_status = 1; ")
    int updateUserPasswordBySecretProtection(@Param("user_id") int user_id,@Param("user_password") String user_password);

    @Select("SELECT user_id FROM `bz_user` WHERE user_name = #{user_name} AND user_account = #{user_account} AND user_password = #{user_password};")
    int selectUserIdByAccountAndPassword(@Param("user_name") String user_name,@Param("user_account") String user_account,@Param("user_password") String user_password);

    @Results({
            @Result(property = "user_id" , column = "user_id"),
            @Result(property = "answer_1_no" , column = "user_security_question1_no"),
            @Result(property = "answer_1" , column = "user_security_question1_answer"),
            @Result(property = "answer_2_no" , column = "user_security_question2_no"),
            @Result(property = "answer_2" , column = "user_security_question2_answer"),
            @Result(property = "answer_3_no" , column = "user_security_question3_no"),
            @Result(property = "answer_3" , column = "user_security_question3_answer"),
    })
    @Select("SELECT user_id,user_security_question1_no,user_security_question1_answer,user_security_question2_no,user_security_question2_answer,user_security_question3_no,user_security_question3_answer from bz_user where user_id = #{user_id};")
    User selectSecretProtectionProblems(@Param("user_id") int user_id);

    /**
     * 根据用户id数组获得用户姓名，头像url数组
     * */
    @Select("<script>" +
            " SELECT user_name,user_headpic_dir" +
            " FROM" +
            " bz_user" +
            " WHERE" +
            " user_status = 1 AND" +
            " user_id IN" +
            " <foreach item='item' index='index' collection='user_ids' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script> ")
    List<Map<String,Object>> selectUsersByUserIds(@Param("user_ids") int[] user_ids);

    /**
     * 根据用户id数组获得用户姓名，头像url数组
     * */
    @Select("<script>" +
            " SELECT user_name" +
            " FROM" +
            " bz_user" +
            " WHERE" +
            " user_status = 1 AND" +
            " user_id IN" +
            " <foreach item='item' index='index' collection='user_ids' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script> ")
    String[]  selectUserNamesByUserIds(@Param("user_ids") int[] user_ids);

    @Select("SELECT user_name from bz_user where user_id = #{user_id} AND user_status = 1")
    String selectUserNameByUserId(String user_id);

    /**
     * 根据用户id数组获得用户邮箱数组
     * */
    @Select("<script>" +
            " SELECT user_email" +
            " FROM" +
            " bz_user" +
            " WHERE" +
            " user_status = 1 AND" +
            " user_id IN" +
            " <foreach item='item' index='index' collection='user_ids' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script> ")
    String[] selectUserEmailsByUserIds(@Param("user_ids") int[] user_ids);

    @Select("SELECT user_status FROM `bz_user` WHERE user_id = #{user_id};")
    int selectUserStatusByUserid(@Param("user_id") int user_id);

    /**
     * 根据用户id数组获得用户名数组
     * */
    @Select("<script>" +
            " SELECT user_name" +
            " FROM" +
            " bz_user" +
            " WHERE" +
            " user_status = 1 AND" +
            " user_id IN" +
            " <foreach item='item' index='index' collection='user_ids' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script> ")
    String[] selectUserNamesByUserIdsForInvite(@Param("user_ids") String[] user_ids);

    /**
     * 完善用户信息
     * */
    @Update("UPDATE bz_user SET user_phone = #{phone_number},user_email = #{email},user_phone = #{phone_number},user_security_question1_no = #{security_question1},user_security_question1_answer = #{security_answer1},user_security_question2_no = #{security_question2},user_security_question2_answer = #{security_answer2},user_security_question3_no = #{security_question3},user_security_question3_answer = #{security_answer3},user_status = 1 WHERE user_id = #{user_id} AND user_status = 4; ")
    int perfectUserInfomation(@Param("user_id") int user_id
                                ,@Param("phone_number") String phone_number
                                ,@Param("email") String email
                                ,@Param("security_question1") int security_question1
                                ,@Param("security_answer1") String security_answer1
                                ,@Param("security_question2") int security_question2
                                ,@Param("security_answer2") String security_answer2
                                ,@Param("security_question3") int security_question3
                                ,@Param("security_answer3") String security_answer3 );

    /**
     * 管理员添加新用户
     * */
    @Insert("INSERT INTO bz_user(user_name,user_account,user_password) VALUES(#{user_name},#{user_account},#{user_password})")
    int insertUser(@Param("user_name") String user_name,@Param("user_account") String user_account, @Param("user_password") String user_password);

    /**
     * 管理员删除用户
     * */
    @Update("UPDATE bz_user SET user_status = 0 WHERE user_id = #{user_id}; ")
    int deleteUser(@Param("user_id") String user_id);
}
